//
//  BKMain_ClassifyKindsHeaderView.m
//  bestkeep
//
//  Created by utouu666 on 17/2/14.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKMain_ClassifyKindsHeaderView.h"
#import "BKMainCommon.h"

@interface BKMain_ClassifyKindsHeaderView()

@property(nonatomic,strong)UIButton *selectedBtn;
@property(nonatomic,copy)NSMutableArray *BtnMutableArray;
@property(nonatomic,strong)NSMutableArray *BtnArray;
@property(nonatomic,assign)BOOL isSeleted;
@property(nonatomic,strong)UIButton *priceLowBtn;

@end

@implementation BKMain_ClassifyKindsHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.isSeleted = NO;
        [self createBtn];
    }
    return self;
}


-(void)createBtn
{
    NSArray *BtnArray = [NSArray arrayWithObjects:@"默认",@"销量",@"新品",@"价格", nil];
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - 3) / 4;
    CGFloat x = [UIScreen mainScreen].bounds.size.width/4;
    _BtnArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 4; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * x, 1, width, 34);
        [btn setTitle:BtnArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:RGB(68, 68, 68) forState:UIControlStateSelected];
        [btn setTitleColor:RGB(136, 136, 136) forState:UIControlStateNormal];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
        btn.tag = 8000 + i;
        [btn addTarget:self action:@selector(targetAction:) forControlEvents:UIControlEventTouchUpInside];
        UIView *separateLine = [[UIView alloc]init];
        separateLine.frame = CGRectMake((i +1) *width, 1 + 1, 0.5, 35 - 4);
        if (i == 1) {
            separateLine.frame = CGRectMake(SCREENWIDTH/2, 1 + 1, 0.5, 35 - 4);
        }
        separateLine.backgroundColor = RGB(218, 218, 218);
        [self addSubview:btn];
        [self addSubview:separateLine];
        [_BtnArray addObject:btn];
        if (i == 0) {
            btn.selected = YES;
            self.selectedBtn = btn;
        }
        if (i == 3) {
            UIButton *lowHighBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [lowHighBtn addTarget:self action:@selector(targetAction:) forControlEvents:UIControlEventTouchUpInside];
            lowHighBtn.backgroundColor = [UIColor whiteColor];
            lowHighBtn.tag = 9000;
            UIImage *img = [UIImage imageNamed:@"BKMain_UpAndDownArrow"];
            [lowHighBtn setBackgroundImage:img forState:UIControlStateNormal];
            [btn addSubview:lowHighBtn];
            [lowHighBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(btn.mas_centerY);
                make.width.equalTo(@(10));
                make.height.equalTo(@(10));
                make.left.equalTo(btn.mas_centerX).offset(20);
            }];
            separateLine.hidden = YES;
            self.priceLowBtn = lowHighBtn;
            
        }
    }
    UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 34, ScreenWidth, 1)];
    bottomLineView.backgroundColor = RGB(238, 238, 238);
    [self addSubview:bottomLineView];
}
-(void)targetAction:(UIButton *)Btn
{
    if (Btn.tag == 9000 || Btn.tag == 8003) {
        
        [self changeBtnImageWithStatus:self.isSeleted];
        Btn.selected = YES;
        self.selectedBtn.selected = NO;
        self.selectedBtn = Btn;

        return;
    }else{
        self.isSeleted = NO;
        [self.priceLowBtn setBackgroundImage:[UIImage imageNamed:@"BKMain_UpAndDownArrow"] forState:UIControlStateNormal];
    }
    
    if (Btn == self.selectedBtn) {
        return;
    }else{
        Btn.selected = YES;
        self.selectedBtn.selected = NO;
        self.selectedBtn = Btn;
        
        if ([self.headerViewDelegate respondsToSelector:@selector(ClassifyKindsHeaderView:index:)]) {
            [self.headerViewDelegate ClassifyKindsHeaderView:self index:Btn.tag - 8000];
            
        }
    }
    
    
}

-(void)changeBtnImageWithStatus:(BOOL)isSeleted
{
    NSString *oderWay = @"";
    if (isSeleted) {
        
        oderWay = @"desc";
        self.isSeleted = NO;
        [self.priceLowBtn setBackgroundImage:[UIImage imageNamed:@"BKMain_arrowDown"] forState:UIControlStateNormal];
    }
    else
    {
        oderWay = @"asc";
        self.isSeleted = YES;
        [self.priceLowBtn setBackgroundImage:[UIImage imageNamed:@"BKMain_arrowUp"] forState:UIControlStateNormal];
    }
    
    if([self.headerViewDelegate respondsToSelector:@selector(ClassifyKindsHeaderView:priceCompare:)])
    {
        [self.headerViewDelegate ClassifyKindsHeaderView:self priceCompare:oderWay];
        
    }
}
//切换商品列表时，重置搜索条件选项 置为全部

-(void)setIsRest:(BOOL)isRest
{
    if (isRest) {
       
        [self resetFirstButtonAndPriceBtn];
    }
    
}
-(void)resetFirstButtonAndPriceBtn
{
    for (UIButton * btn in self.BtnArray) {
        
        if (btn.tag == 8000) {
            
            btn.selected = YES;
            self.selectedBtn = btn;
            
            UIImage *badk =[UIImage imageNamed:@"BKMain_UpAndDownArrow"];
            [_priceLowBtn setBackgroundImage:badk forState:(UIControlStateNormal)];
            
            
        } else {
            
            btn.selected = NO;
        }
    }
    
}
-(void)recordSearchConditionWithSearchCondition:(NSString *)searchCondition order:(NSString *)order
{
    
    for (UIButton * btn in self.BtnArray) {
        
        if ([searchCondition isEqualToString:@""] && btn.tag == 8000) {
            
            btn.selected = YES;
            self.selectedBtn = btn;
            
        }
        else if ([searchCondition isEqualToString:@"saledCount"] && btn.tag == 8001) {
            
            btn.selected = YES;
//            self.selectedBtn.selected = NO;
            self.selectedBtn = btn;
            
            
        }else if ([searchCondition  isEqualToString:@"newGoods"] && btn.tag == 8002) {
            
            btn.selected = YES;
//            self.selectedBtn.selected = NO;
            self.selectedBtn = btn;
            
            
        } else if([searchCondition  isEqualToString:@"price"] && btn.tag == 8003){
            
            btn.selected = YES;
//            self.selectedBtn.selected = NO;
            self.selectedBtn = btn;
            
            
        }else{
            btn.selected = NO;
            
        }
        
        if ([order isEqualToString:@"desc"]) {
            
            [_priceLowBtn setBackgroundImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
        }
        
        if ([order isEqualToString:@"asc"]) {
            
            [_priceLowBtn setBackgroundImage:[UIImage imageNamed:@"arrowUp"] forState:UIControlStateNormal];
            
        }
        
        if ([order isEqualToString:@""]) {
            
            [_priceLowBtn setBackgroundImage:[UIImage imageNamed:@"UpAndDownArrow"] forState:UIControlStateNormal];
        }
        
        
    }
    
}

@end
